﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalemShop.Forms
{
    public partial class FrmInvoiceDetail : FrmBaseDock
    {
        private FrmInvoiceExplorer frmExp = null;

        private long invoiceId;

        public FrmInvoiceDetail()
        {
            InitializeComponent();
        }

        public FrmInvoiceDetail(FrmCalemShop shop)
            : base(shop)
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        public FrmInvoiceExplorer InvoiceExplorer
        {
            set { this.frmExp = value; }
            get { return this.frmExp; }
        }

        public int InvoiceId
        {
            set
            {
                try
                {
                    this.invoiceId = value;
                    this.Text = string.Format("Chi tiết hóa đơn {0}", value);
                    this.TabText = this.Text;
                    LoadInvoiceRows();
                }
                catch (Exception e) { }
            }
        }


        private void LoadInvoiceRows()
        {
            DataTable dt = Database.getInvoiceBuying((int)invoiceId);
            if (dt == null || dt.Rows.Count == 0)
            {
                ResetInvoiceData();
                return;
            }


            cartView.Items.Clear();
            string seller = string.Empty;
            string createDate = string.Empty;
            foreach (DataRow r in dt.Rows)
            {
                int stt = cartView.Items.Count + 1;
                decimal price = decimal.Parse(r["price"].ToString());
                decimal money = decimal.Parse(r["total"].ToString());
                decimal ck = decimal.Parse(r["ck"].ToString());
                ListViewItem item = new ListViewItem(new string[] {stt.ToString(), 
                    r["itemId"].ToString(),  r["itemName"].ToString(),
                    r["amount"].ToString(), price.ToString("#,0"), money.ToString("#,0"),  ck.ToString("#,0") + "%"});
                item.SubItems[clPrice.Index].Tag = price;
                item.SubItems[clMSP.Index].Tag = r["itemId"];
                item.SubItems[clDiscount.Index].Tag = r["ck"];
                cartView.Items.Add(item);
                item.Tag = r["autoKey"];
                if (string.IsNullOrEmpty(seller))
                {
                    seller = r["seller"].ToString();
                }

                if (string.IsNullOrEmpty(createDate))
                {
                    createDate = r["invoiceDate"].ToString();
                }
            }

            decimal totalMoney = Database.getInvoiceBuyingTotalMoney((int)invoiceId);
            if (totalMoney < 0)
            {
                txtMoney.ForeColor = Color.Red;
            }
            else
            {
                txtMoney.ForeColor = SystemColors.ControlText;
            }

            txtMoney.Text = string.Format("{0} VNĐ", totalMoney.ToString("#,0"));
            txtStaff.Text = seller;
            txtDate.Text = string.Format("Ngày lập : {0}", createDate);
        }

        private void ResetInvoiceData()
        {
            txtStaff.Text = string.Empty;
            txtMoney.Text = string.Empty;
            txtDate.Text = "Ngày lập :";
            cartView.Items.Clear();
        }
    }
}
